#include<bits/stdc++.h>
#define ONLINE_JUDGE
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 10, INF = 0x3f3f3f3f;

int n, gm, hm, ans = INF;
int hs[N];
int g[N][N], h[N][N];
int a[N][N];
bool st[N];

void print()
{
    for (int i = 1;i <= n;i++) cout << hs[i] << ' ';
    cout << '\n';
}

void dfs(int x)
{
    if (x == n + 1) {
        int sum = 0;
        for (int i = 1;i <= n;i++)
            for (int j = i;j <= n;j++)
                if (g[i][j] != h[hs[i]][hs[j]]) sum += a[hs[i]][hs[j]];
        //if (sum == 0) print();
        ans = min(ans, sum);
        return;
    }
    for (int i = 1;i <= n;i++) {
        if (st[i]) continue;
        hs[x] = i;st[i] = 1;
        dfs(x + 1);
        st[i] = 0;
    }
}

void solve()
{
    cin >> n >> gm;
    for (int i = 1;i <= gm;i++) {
        int x, y;cin >> x >> y;
        g[x][y] = g[y][x] = 1;
    }
    cin >> hm;
    for (int i = 1;i <= hm;i++) {
        int x, y;cin >> x >> y;
        h[x][y] = h[y][x] = 1;
    }
    for (int i = 1;i < n;i++)
        for (int j = i;j <= n;j++) {
            if (i == j) continue;
            cin >> a[i][j];
            a[j][i] = a[i][j];
        }
    
    // for (int i = 1;i <= n;i++) {
    //     for (int j = 1;j <= n;j++)
    //         cout << a[i][j] << ' ';
    //     cout << '\n';
    // }
    // cout << '\n';

    dfs(1);
    cout << ans << '\n';
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}